package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.VideoAdapter;
import flc.ast.databinding.ActivityVideoBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class VideoActivity extends BaseAc<ActivityVideoBinding> {

    private VideoAdapter videoAdapter;
    private boolean isEdit = false;
    private List<String> listUri = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_video;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            cancelEdit();
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivVideoEdit.setOnClickListener(this);
        mDataBinding.tvVideoDelete.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new VideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivVideoEdit://编辑
                isEdit = true;
                mDataBinding.ivVideoEdit.setVisibility(View.GONE);
                mDataBinding.tvVideoDelete.setVisibility(View.VISIBLE);
                videoAdapter.setEdit(isEdit);
                videoAdapter.notifyDataSetChanged();
                mDataBinding.tvVideoDelete.setText(getString(R.string.delete_text, listUri.size()));
                break;
            case R.id.tvVideoDelete://删除
                if (listUri.size() == 0) return;
                deleteVideo();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (videoAdapter.getItem(position).isChecked()) {
                videoAdapter.getItem(position).setChecked(false);
                listUri.remove(videoAdapter.getItem(position).getUri());
            } else {
                videoAdapter.getItem(position).setChecked(true);
                listUri.add(videoAdapter.getItem(position).getUri());
            }
            videoAdapter.notifyDataSetChanged();
            mDataBinding.tvVideoDelete.setText(getString(R.string.delete_text, listUri.size()));
        } else {
            Uri videoUri = Uri.parse(videoAdapter.getItem(position).getUri());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(videoUri, "video/*");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //删除视频
    private void deleteVideo() {

        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (String uri : listUri) {
                    mContext.getContentResolver().delete(Uri.parse(uri), null, null);
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        cancelEdit();
                        getData();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                    }
                }, 500);
            }
        });
    }

    //取消编辑
    private void cancelEdit() {
        listUri.clear();
        isEdit = false;
        mDataBinding.ivVideoBack.setVisibility(View.VISIBLE);
        mDataBinding.ivVideoEdit.setVisibility(View.VISIBLE);
        mDataBinding.tvVideoDelete.setVisibility(View.GONE);
        videoAdapter.setEdit(isEdit);
        for (SelectMediaEntity bean : videoAdapter.getValidData()) {
            bean.setChecked(false);
        }
        videoAdapter.notifyDataSetChanged();
    }

}